package com.sam;


import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.IBinder;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;

public class BindingActivity extends Activity {
	SocketService mBoundService;
	boolean mIsBound = false;
	private ServiceConnection mConnection = new ServiceConnection() {
		// EDITED PART
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			mBoundService = ((SocketService.LocalBinder) service).getService();	
			mBoundService.startTcp();
			
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			mBoundService = null;
		}

	};
	
	
	@SuppressWarnings("unused")
	private void doBindService() {
		bindService(new Intent(this, SocketService.class), mConnection,
				Context.BIND_AUTO_CREATE);
		mIsBound = true;
		if (mBoundService != null) {
			mBoundService.IsBoundable();
		}
	}

	@SuppressWarnings("unused")
	private void doUnbindService() {
		if (mIsBound) {
			unbindService(mConnection);
			mIsBound = false;
		}
	}

	@SuppressWarnings("deprecation")
	@SuppressLint("ShowToast")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);	
	WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
	//if (wifi.isWifiEnabled()){
		Intent i=new Intent(this, SocketService.class); 
		  startService(i);
		  finish();
	//}
	//else{
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("Turn On Wifi");
		alertDialog.setMessage("please turn on the wifi first");
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
		  finish();
		}
		});
		alertDialog.setIcon(R.drawable.sam_launcher);
		alertDialog.show();
	//}
	   
		  
	}
	
	
}
